<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 投票ajax类
 * @package  ajax
 * @subpackage  backend
 * @author   create by allen.qiang
 */
require 'module/base/contentHandler.class.php';

class ajax_vote {
	//入口
	public function execute(){
		$operFlg = isset($_REQUEST['operFlg']) ? $_REQUEST['operFlg'] : null;
		switch ($operFlg){
			case 1:
				$this->showVoteList();
				break;
			case 2:
			case 3:
				$this->editVoteList();
				break;
			case 4:
				$this->showResult();
				break;
			case 5:
				$this->delete();
				break;
		}
	}
	
	//显示查询列表
	private function showVoteList(){
		global $G_PGLOB;
		
		//当前页
		$page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 1;
		
		//每页显示条数
		$rp = isset($_REQUEST['rp']) ? $_REQUEST['rp'] : 10;
		
		//排序的列的名称
		$sortname = isset($_REQUEST['sortname']) ? $_REQUEST['sortname'] : 'timeStamp';
		
		//排序方式（正序、倒序）
		$sortorder = isset($_REQUEST['sortorder']) ? $_REQUEST['sortorder'] : 'DESC';
		
		//投票分类
		$vote_category = (isset($_REQUEST['vote_category']) && is_numeric($_REQUEST['vote_category'])) ? $_REQUEST['vote_category'] : '';
		
		$importData = array('page' 				 => $page,
							'rp'                 => $rp,
							'sortname'           => $sortname,
							'sortorder'          => $sortorder,
							'vote_category'      => $vote_category);
		
		//实例化投票公共类
		$contentHandler = new contentHandler($G_PGLOB->DBCA);
		
		//调用searchAllVote()查询全部投票
		$result_search = $contentHandler->searchVoteInfo($importData);
		
		//页面输出数组
		$result_array = array(
				'page' => $_REQUEST['page'],
				'total' => $result_search['message']['total'],
				'rows' => array()
		);
		
		//将查询结果组装进页面输出数组中
		if ($result_search['code'] == 200 && !empty($result_search['message']['data'])){
			
			$i = 0;
			
			foreach($result_search['message']['data'] as $voteList){
				
				if ($voteList->can_multi == 1){
					$can_multi = '可以';
				}else {
					$can_multi = '不可';
				}
				
				switch ($voteList->vote_category){
					case 1:
						$vote_category = '首页问卷';
						break;
					case 2:
						$vote_category = '会员中心';
						break;
					case 3:
						$vote_category = '分类调查';
						break;
					case 4:
						$vote_category = '购物流程';
						break;
				}
				
				$result_array['rows'][] = array(
						'id' => $voteList->uuid,
						'cell' => array(
								'<a href="javascript:void(0);" onclick="source.detail('.$i.');">查看</a>&nbsp;'.
								'<a href="javascript:void(0);" onclick="source.edit('.$i.');">修改</a>',
								$vote_category,
								$voteList->vote_name,
								$voteList->start_time,
								$voteList->end_time,
								$voteList->vote_count,
								$can_multi
						)
				);
				
				$i++;
			}
		}
		echo json_encode($result_array);
	}
	
	//新增、编辑投票
	private function editVoteList () {
		global $G_PGLOB;
		
		//operFlg(2新增，3修改)
		$operFlg = isset($_REQUEST['operFlg']) ? $_REQUEST['operFlg'] : null;
		//投票分类
		$vote_category = isset($_REQUEST['vote_category']) ? $_REQUEST['vote_category'] : null;
		//投票主题
		$vote_name = isset($_REQUEST['vote_name']) ? $_REQUEST['vote_name'] : null;
		//投票uuid
		$vote_uuid = isset($_REQUEST['vote_uuid']) ? $_REQUEST['vote_uuid'] : null;
		//开始时间
		$start_time = isset($_REQUEST['start_time']) ? $_REQUEST['start_time'] : null;
		//结束时间
		$end_time = isset($_REQUEST['end_time']) ? $_REQUEST['end_time'] : null;
		//投票选项
		$options = isset($_REQUEST['options']) ? $_REQUEST['options'] : null;
		//是否多选
		$can_multi = isset($_REQUEST['can_multi']) ? $_REQUEST['can_multi'] : null;
		
		$importData = array('operFlg'       => $operFlg,
							'creator'       => $G_PGLOB->currentUserID,
							'editor'        => $G_PGLOB->currentUserID,
							'vote_category' => $vote_category,
							'vote_name'     => $vote_name,
							'vote_uuid'     => $vote_uuid,
							'start_time'    => $start_time,
							'end_time'      => $end_time,
							'options'       => $options,
							'can_multi'     => $can_multi);
		
		$contentHandler = new contentHandler($G_PGLOB->DBCA);
		
		$result_edit = $contentHandler->editVote($importData);
		
		echo json_encode($result_edit);
	}
	
	//显示投票结果
	private function showResult(){
		global $G_PGLOB;
		
		//投票uuid
		$vote_uuid = isset($_REQUEST['vote_uuid']) ? trim($_REQUEST['vote_uuid']) : null;
		
		//排序的列的名称
		$sortname = isset($_REQUEST['sortname']) ? trim($_REQUEST['sortname']) : '';
		
		//排序方式（正序、倒序）
		$sortorder = isset($_REQUEST['sortorder']) ? trim($_REQUEST['sortorder']) : '';
		
		$importData = array('vote_uuid' => $vote_uuid, 'sortname' => $sortname, 'sortorder' => $sortorder);
		
		$contentHandler = new contentHandler($G_PGLOB->DBCA);
		
		$result_vote = $contentHandler->searchAllVoteInfo($importData);
		
		//页面输出数组
		$result_array = array(
				'page' => isset($_REQUEST['page']) ? $_REQUEST['page'] : '',
				'total' => $result_vote['message']['total'],
				'rows' => array()
		);
		
		if ($result_vote['code'] == 200 && !empty($result_vote['message']['data'])){
			
			$totalNum = 0;
			
			foreach($result_vote['message']['data'] as $voteList){
				$totalNum += $voteList->option_count;
			}
			
			foreach($result_vote['message']['data'] as $voteList){
				$result_array['rows'][] = array(
						'id' => $voteList->uuid,
						'cell' => array(
								$voteList->option_name,
								empty($voteList->option_count) ? 0 : number_format($voteList->option_count / $totalNum * 100 , 2) .'%',
								empty($voteList->option_count) ? 0 : $voteList->option_count
						)
				);
			}
			
		}
		
		echo json_encode($result_array);
	}
	
	//删除投票
	private function delete(){
		global $G_PGLOB;
		//错误信息
		$error_str = '';
		//uuid组成的数组
		$vote_uuid = isset($_REQUEST['vote_uuid']) ? $_REQUEST['vote_uuid'] : null;
		
		if (empty($vote_uuid)){
		
			$error_str .= '传值错误 ，未接收到选择的数据';
		
			$error_msg = array('code' => 400, 'message' => $error_str);
			echo json_encode($error_msg);
		
		}else {
		
			$uuid_arr = explode(',', $vote_uuid);
		
			$contentHandler = new contentHandler($G_PGLOB->DBCA);
		
			$result = $contentHandler->delVote($uuid_arr);
		
			echo json_encode($result);
		}
		
	}
}